<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class My  extends Controller
{

    public function my(Request $request)
    {
        $uid = session('uid');
        $us = Db::name('user')->where('id', $uid)->find();
        if ($us) {

            $a = "你好！打开音乐让自己放松一下吧";
            // $user= $us['username'];
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            $us['avatar'] = $qiniu_config['domain'] . '/' . $us['avatar'] . $q_thumb;
            // $user=Db::name('user')->where(['id'=>66, 'phone'])->find();;
        }

        $usr = json_encode($us);
//        if(!cmf_is_wechat()){
//            $this->error('需要微信中打开');
//        }
        $weixin = new Weixin();
        $url = $request->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);

        $this->assign("us", $usr);
        $this->assign("uid", $uid);
        $this->assign('jsconfig', $js_config);
        return $this->fetch('mine');
    }
    //搜索  href="{:url('/home/My/sousuo')}"


    //设置
    public  function  grzl(){
        return  $this->fetch('grzl');
    }

    //退出登录
    public   function  loginout(){
        Session::clear();
        $this->redirect('/home/shouye/shouye');
       // $this->redirect('my');
    }
    //修改密码
    public  function  xgmm(){
        $uid = session('uid');
        $us = Db::name('user')->where('id', $uid)->find();
        $this->assign("us", $us);
        return  $this->fetch('xgmm');
    }

    public function xg(){
      $uid = session('uid');
      $p= Db::name('user')->where('id',$uid)->find();
      $mobile=$p["phone"];

      if($this->request->isAjax()){
        //  $password = input('mima');


          $password =hash('md5',input('mima'));
          $res= Db::name('user')->where('id', $uid)->update(['password' => $password]);
          if ($res == 1) {
              $this->success('发送成功');
          }else{
              $this->error('修改密码失败');
          }


//          $mobile_code = input()['code'];
//          if (!$mobile || !$mobile_code) {
//              $this->error('请输入手机号或验证码');
//          }
//          $res1 = Db::name('sms_code')->where('phone', $mobile)->find();
//
//          if ($res1) {
//              //是否过期
//              if ($res1['createtime'] < (time() - 60 * 10)) {
//                  $this->error('验证码过期');
//              }
//              if ($res1['code'] != $mobile_code) {
//                  $this->error('验证码错误');
//              }
//              $user = Db::name('user')->where('phone', $mobile)->find();
//              session('uid', $user['id']);
//              session('user', $user);
//              $this->success('认证成功');
//
//          } else {
//              $this->error('验证码错误2');
//          }
//
      }
  }

    public function sms()
    {
        $uid = session('uid');
        $p= Db::name('user')->where('id',$uid)->find();
        $phone=$p["phone"];


        if (!isMobileNum($phone)) {
            $this->error('手机号不正确');
        };

        // 判断是否存在
        $res = Db::name('user')->where('phone', $phone)
            ->field('id')
            ->find();
        if (!$res) {
            $this->error('手机号未认证');
        }

        $res = sendSms($phone);
        if ($res == 1) {
            $this->success('发送成功');
        }
        $this->error('发送失败');
    }

    public  function  sousuo(){
        return  $this->fetch('sousuo');
    }
    //我的订单
    public function myorder()
    {
        return  $this->fetch('my_order');
    }
    //优惠券操作
//    public function mycoupon()
//    {
//        if(!cmf_is_wechat()){
//            $this->error('需要微信中打开');
//        }
//        $weixin = new Weixin();
//       $url = $this->request()->domain().$this->request->url();
//       $js_config =$weixin->js_config($url);
//       $this->assign('jsconfig',$js_config);
//
//        return $this->fetch();
//
//        //return  $this->fetch('my_coupon');
//    }
    //质检
    public function myjgprogram(Request $request)

    {
        if ($request->isAjax()){

            $status[0] = '禁用';
            $status[1] = '正常';
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 5;
            $flag=input('flag');
            if($flag == '1'){
                $fenlei = input('phone');
//                var_dump($fenlei).die();
                if($fenlei){
                    $res = Db::name('project')
                        ->join('iw_project_client', 'iw_project_client.project_id=iw_project.id')
                        ->where([
                            'iw_project.address'=>['like', '%' . $fenlei . '%']
                        ])
                        ->page($page, $limit)
                        ->select();

                    if ($res) {
                        $qiniu_config = cmf_get_option('qiniu');
                        // 七牛样式处理
                        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                        foreach ($res as $index => $re) {
                            $res[$index]['project_picture'] = $qiniu_config['domain'] . '/' . $re['project_picture'] . $q_thumb;
                        }
                        return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
                    }
                }
            }

          else{
                $res = Db::name('project')
                    ->join('iw_project_client', 'iw_project_client.project_id=iw_project.id')
                    ->page($page, $limit)->select();
            if ($res) {
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                foreach ($res as $index => $re) {
                    $res[$index]['project_picture'] = $qiniu_config['domain'] . '/' . $re['project_picture'] . $q_thumb;
                }
                return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
            }

          }

        }
        return  $this->fetch('my_jg_program');
    }

    //客户经理
    public function mykhjlprogram(Request $request)

    {
        if ($request->isAjax()){
            $status[0] = '禁用';
            $status[1] = '正常';
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 5;
            $flag=input('flag');
            if($flag == '1'){
                $fenlei = input('key');
//                var_dump($fenlei).die();
                if($fenlei){
                    $res = Db::name('project')
                        ->join('iw_project_client', 'iw_project_client.project_id=iw_project.id')
                        ->where([
                            'iw_project.address'=>['like', '%' . $fenlei . '%']
                        ])
                        ->page($page, $limit)
                        ->select();

                    if ($res) {
                        $qiniu_config = cmf_get_option('qiniu');
                        // 七牛样式处理
                        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                        foreach ($res as $index => $re) {
                            $res[$index]['project_picture'] = $qiniu_config['domain'] . '/' . $re['project_picture'] . $q_thumb;
                        }
                        return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
                    }
                }
            }

            else{
                $res = Db::name('project')
                    ->join('iw_project_client', 'iw_project_client.project_id=iw_project.id')
                    ->page($page, $limit)->select();
                if ($res) {
                    $qiniu_config = cmf_get_option('qiniu');
                    // 七牛样式处理
                    $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                    foreach ($res as $index => $re) {
                        $res[$index]['project_picture'] = $qiniu_config['domain'] . '/' . $re['project_picture'] . $q_thumb;
                    }
                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
                }

            }

        }
        return  $this->fetch('my_khjl_program');
    }
    //项目经理
    public function myprogram(Request $request)
    {
//        if ($request->isAjax()){
//            $status[0] = '禁用';
//            $status[1] = '正常';
//            $page = input('page') ? input('page') : 1;
//            $limit = input('limit') ? input('limit') : 5;
//            $flag1 = $this->request->param('flag');
//                if ( $flag1 == '0' ){
//                    $res = Db::name('project')
//                        ->join('iw_project_client', 'iw_project_client.project_id=iw_project.id')
//                        ->where('status' ,0)  ->page($page, $limit)->select();
//                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
//                }
//
//                else if ( $flag1 == '1' ){
//                    $res = Db::name('project')->where('status' ,1)  ->page($page, $limit)->select();
//                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
//                }
//
//                else if ($flag1 == '2'){
//                    $res = Db::name('project')->where('status' ,2)->page($page, $limit)->select();
//                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
//                } else if ( $flag1 == '3'){
//                    $res = Db::name('project')->where('status' ,3)->page($page, $limit)->select();
//                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
//                }else if ( $flag1 == '4'){
//                    $res =Db::name('project')
//                        ->join('iw_project_client', 'iw_project_client.project_id=iw_project.id')
//                        ->page($page, $limit)->select();
//                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
//                }
//                 else {
//                    $res = Db::name('project')
//                        ->join('iw_project_client', 'iw_project_client.project_id=iw_project.id')
//                        ->page($page, $limit)->select();
//                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
//                }
//
//        }

        if ($request->isAjax()){
            $status[0] = '禁用';
            $status[1] = '正常';
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 5;
            $flag=input('flag');
            if($flag == '1'){
                $fenlei = input('key');
//                var_dump($fenlei).die();
                if($fenlei){
                    $res = Db::name('project')
                        ->join('iw_project_client', 'iw_project_client.project_id=iw_project.id')
                        ->where([
                            'iw_project.address'=>['like', '%' . $fenlei . '%']
                        ])
                        ->page($page, $limit)
                        ->select();

                    if ($res) {
                        $qiniu_config = cmf_get_option('qiniu');
                        // 七牛样式处理
                        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                        foreach ($res as $index => $re) {
                            $res[$index]['project_picture'] = $qiniu_config['domain'] . '/' . $re['project_picture'] . $q_thumb;
                        }
                        return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
                    }
                }
            }

            else{
                $res = Db::name('project')
                    ->join('iw_project_client', 'iw_project_client.project_id=iw_project.id')
                    ->page($page, $limit)->select();
                if ($res) {
                    $qiniu_config = cmf_get_option('qiniu');
                    // 七牛样式处理
                    $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                    foreach ($res as $index => $re) {
                        $res[$index]['project_picture'] = $qiniu_config['domain'] . '/' . $re['project_picture'] . $q_thumb;
                    }
                    return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
                }

            }

        }



        return  $this->fetch('my_program');
    }
    //装修费用计算、
    public function mycostcount()
    {
        return  $this->fetch('my_costcount');
    }
    //扫描优惠券


}


